import Vue from 'vue'
import App from './App'

const tabBarLinks = [];

// 工具类
import util from './utils/util.js';

Vue.config.productionTip = false

// #ifdef APP-PLUS
// 版本号
const currentVersion = {
	id: plus.runtime.version
}
Vue.prototype.$current = currentVersion
// #endif

Vue.prototype.api_root = 'http://127.0.0.1:9053/index.php/';

/**
 * 获取商城ID
 */ 
Vue.prototype.getWxappId = function() {
	return 10001;
}


/**
 * 获取tabBar页面路径列表
 */
Vue.prototype.getTabBarLinks = function() {
	return tabBarLinks;
}
/**
 * 记录推荐人id
 */
Vue.prototype.saveRefereeId = function(refereeId) {
	if (!uni.getStorageSync('referee_id'))
		uni.setStorageSync('referee_id', refereeId);
}
/**
 * 获取场景值(scene)
 */
Vue.prototype.getSceneData = function(query) {
	return query.scene ? util.scene_decode(query.scene) : {};
}

/**
 * 生成转发的url参数
 */

Vue.prototype.getShareUrlParams = function(params) {
	let _this = this;
	return util.urlEncode(Object.assign({
		referee_id: _this.getUserId()
	}, params));
}


/**
 * 设置顶部导航栏
 */
Vue.prototype.setCommonPageBar = function(page) {
	uni.setStorageSync('titleTextColor',page.style.titleTextColor === 'white' ? '#ffffff' : '#000000');
	uni.setStorageSync('titleBackgroundColor',page.style.titleBackgroundColor);
}
/**
 * 获取顶部导航栏
 */
Vue.prototype.getCommonPageBar = function() {
	return {
		titleTextColor: uni.getStorageSync('titleTextColor'),
		titleBackgroundColor: uni.getStorageSync('titleBackgroundColor'),
	}
}

/**
 * 记录formId
 * (因微信模板消息已下线，所以formId取消不再收集)
 */
Vue.prototype.saveFormId = function(formId) {
	return true;
}

// 模拟继承微信setData的方法
Vue.prototype.setData = function(obj) {
	// console.log('这里是方法');
	let that = this;
	let keys = [];
	let val, data;
	Object.keys(obj).forEach(function(key) {
		keys = key.split('.');
		val = obj[key];
		data = that.$data;
		keys.forEach(function(key2, index) {
			// console.log(key2)
			if (index + 1 == keys.length) {
				that.$set(data, key2, val);
			} else {
				if (!data[key2]) {
					that.$set(data, key2, {});
				}
			}
			data = data[key2];
		})
	});
}

// 显示失败提示框
Vue.prototype.showError = function(msg, callback) {
	uni.showModal({
		title: '友情提示',
		content: msg,
		showCancel: false,
		success: function(res) {
			// callback && (setTimeout(function() {
			//   callback();
			// }, 1500));
			callback && callback();
		}
	});
}
/**
 * 显示成功提示框
 */
Vue.prototype.showSuccess = function(msg, callback) {
	uni.showToast({
		title: msg,
		icon: 'success',
		mask: true,
		duration: 1500,
		success: function() {
			callback && (setTimeout(function() {
				callback();
			}, 1500));
		}
	});
}

/**
 * 验证是否存在user_info
 */
Vue.prototype.validateUserInfo = function() {
	let user_info = uni.getStorageSync('user_info');
	return !!uni.getStorageSync('user_info');
}



/**
 * 跳转到指定页面
 * 支持tabBar页面
 */
Vue.prototype.navigationTo = function(url) {
	let urls = url.url
	if (!urls || urls.length == 0) {
		return false;
	}
	// console.log(urls);
	let tabBarLinks = this.getTabBarLinks();
	// tabBar页面
	if (tabBarLinks.indexOf(urls) > -1) {
		uni.switchTab({
			url: '/' + urls
		});
	} else {
		// 普通页面
		uni.navigateTo({
			url: '/' + urls
		});
	}
}
// 模拟继承get请求的方法
Vue.prototype._get = function(url, data, success, fail, complete, check_login) {
	let App = this;
	// 构造请求参数
	data = data || {};

	// 构造get请求
	let request = function() {
		data.token = uni.getStorageSync('token');
		// data.token = 'b632c206a4b12399578f34585d2c7769';
		uni.request({
			url: App.api_root + url,
			header: {
				'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
			},
			data: data,
			success: function(res) {
				if (res.statusCode !== 200 || typeof res.data !== 'object') {
					console.log(res);
					App.showError('网络请求出错');
					return false;
				}
				if (res.data.code === -1) {

					// 登录态失效, 重新登录
					App.doLogin();
				} else if (res.data.code === 0) {
					App.showError(res.data.msg, function() {
						fail && fail(res);
					});
					return false;
				} else {
					success && success(res.data);
				}
			},
			fail: function(res) {
				App.showError(res.errMsg, function() {
					fail && fail(res);
				});
			},
			complete: function(res) {
				uni.hideNavigationBarLoading();
				complete && complete(res);
			},
		});
	};
	// 判断是否需要验证登录
	check_login ? App.doLogin(request) : request();
}

// 模拟继承post请求的方法
Vue.prototype._post_form = function(url, data, success, fail, complete) {
	let App = this;
	data.token = uni.getStorageSync('token');
		// data.token = 'b632c206a4b12399578f34585d2c7769';

	uni.request({
		url: App.api_root + url,
		header: {
			'content-type': 'application/x-www-form-urlencoded',
		},
		method: 'POST',
		data: data,
		success: function(res) {
			if (res.statusCode !== 200 || typeof res.data !== 'object') {
				App.showError('网络请求出错');
				return false;
			}
			if (res.data.code === -1) {

				// 登录态失效, 重新登录
				App.doLogin();
				return false;
			} else if (res.data.code === 0) {
				App.showError(res.data.msg, function() {
					fail && fail(res);
				});
				return false;
			}
			success && success(res.data);
		},
		fail: function(res) {
			// console.log(res);
			App.showError(res.errMsg, function() {
				fail && fail(res);
			});
		},
		complete: function(res) {
			// uni.hideLoading();
			complete && complete(res);
		}
	});
}


// 参数number为毫秒时间戳，format为需要转换成的日期格式
Vue.prototype.transformTime = function(timestamp) {
	// 获取年月日时分秒值  slice(-2)过滤掉大于10日期前面的0
	var datetime = new Date(timestamp * 1000);
	var year = datetime.getFullYear(),
		month = ("0" + (datetime.getMonth() + 1)).slice(-2),
		date = ("0" + datetime.getDate()).slice(-2),
		hour = ("0" + datetime.getHours()).slice(-2),
		minute = ("0" + datetime.getMinutes()).slice(-2),
		second = ("0" + datetime.getSeconds()).slice(-2);
	// 拼接
	console.log('year====>' + datetime);
	var result = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
	// 返回
	return result;
}


Vue.prototype.showModal = function(msg, callback) {
	// #ifdef APP-PLUS
	uni.showModal({
		title: '友情提示',
		content: msg,
		cancelText: '确认',
		confirmText: '取消',
		success: function(res) {
			if (res.cancel) callback && callback(res);
		}
	});
	return;
	// #endif

	// #ifdef MP-WEIXIN
	uni.showModal({
		title: '友情提示',
		content: msg,
		success: function(res) {
			if (res.confirm)  callback && callback(res);
		},
	});
	return;
	// #endif
}

/* 打开外部浏览器 */
Vue.prototype.openBrowser = function(url){
	// #ifdef MP-WEIXIN
		return;
	// #endif
	
	// #ifdef APP-PLUS
		plus.runtime.openURL(url);
		return;
	// #endif
	
	// #ifdef H5
		window.location.href = url;
		return;
	// #endif
}
Vue.prototype.showLoading = function(title = '加载中', callback) {
	uni.showLoading({
		title: title,
		mask: true,
		success: function(res) {
			if (res.cancel) callback && callback(res);
		}
	});
	return;
}

Vue.prototype.hideLoading = function() {
	uni.hideLoading();
	return;
}


App.mpType = 'app'

const app = new Vue({
	...App
})
app.$mount()
